#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import os
import sys
import string
import locale
language, output_encoding = locale.getdefaultlocale()
class dicts:
  def __init__(self, dictfile):
    self.dictfile= open (dictfile, "r")
    self.words={}
    self.populate()
  def populate(self):
    for line in self.dictfile.readlines():
      linewords = line.rstrip().lower().translate(None, string.punctuation).translate(None, string.digits).split()
      for lineword in linewords:
        self.words[lineword] = lineword
  def hasWord(self, word):
    if word.lower() in self.words:
      return True
    else:
      return False
  def getWords(self):
    return self.words

class words:
  def __init__(self, wordfile):
    self.wordfile= open (wordfile, "r")
    self.words=[]
    self.populate()
  def populate(self):
    for line in self.wordfile.readlines():
      linewords = re.findall(r'[a-záðéíóúýþæöA-ZÁÐÉÍÓÚÝÞÆÖ]+', line)
      for lineword in linewords: 
        if lineword != '':
          self.words.append(lineword)
  def getWords(self):
    return self.words

class processWords:
  def __init__(self, iswords, enwords, filewords):
    self.totalIsWords = 0
    self.totalEnWords = 0
    self.totalUnknownWords = 0
    self.pctIsWords=0
    self.pctEnWords=0
    self.longestIsWord=0
    self.averageIsWord=0
    self.isWordTotalLength=0
    self.freq={}
    for i in range(1, 28):
      self.freq[i]=0
    self.process(iswords, enwords, filewords)
  def process(self, iswords, enwords, filewords):
    for key in filewords.getWords():
      if iswords.hasWord(key):
        print key
        self.freq[len(key)] += 1
        self.totalIsWords += 1
        self.isWordTotalLength += len(key.decode("utf-8"))
        if len(key) > self.longestIsWord:
          self.longestIsWord = len(key.decode("utf-8"))
      elif enwords.hasWord(key):
        self.totalEnWords += 1
      else:
        self.totalUnknownWords += 1
  def getTotalWords(self):
    self.totalWords = self.totalIsWords + self.totalEnWords + self.totalUnknownWords
    return self.totalWords
  def getTotalIsWords(self):
    return self.totalIsWords
  def getTotalEnWords(self):
    return self.totalEnWords
  def getTotalUnknownWords(self):
    return self.totalUnknownWords
  def getPctIsWords(self):
    return (self.totalIsWords*100)/float(self.totalWords)
  def getPctEnWords(self):
    return (self.totalEnWords*100)/float(self.totalWords)
  def getPctUnkownWords(self):
    return (self.totalUnknownWords*100)/float(self.totalWords)
  def getLongestIsWord(self):
    return self.longestIsWord
  def getAverageIsWord(self):
    return self.isWordTotalLength/float(self.totalIsWords)
  def getReport(self):
    report=[]
    report.append("-- Report --")
    report.append("Inputfile: %s" % (sys.argv[1]))
    report.append("Total number of words: %s" % (self.getTotalWords()))
    report.append("Total number of icelandic words: %s" % (self.getTotalIsWords()))
    report.append("Percentage of icelandic words: %s %s" % ("{0:.2f}".format(self.getPctIsWords()),"%"))
    report.append("Longest icelandic word: %s" % (self.getLongestIsWord()))
    report.append("Average icelandic word length: %s" % ("{0:.2f}".format(self.getAverageIsWord())))
    report.append("Total number of english words: %s" % (self.getTotalEnWords())) 
    report.append("Percentage of english words: %s %s" % ("{0:.2f}".format(self.getPctEnWords()),"%"))
    report.append("Total number of unknown words: %s" % (self.getTotalUnknownWords()))
    report.append("Percentage of unknown words: %s %s" % ("{0:.2f}".format(self.getPctUnkownWords()),"%"))
    return '\n'.join(report)
  def getFrequency(self):
    w= open ("wordLengthSampleFreq.txt", "w")
    for i in range(2, 28):
      x=("%d	%f\n" % (i,(self.freq[i]*100)/float(self.getTotalIsWords())))
      w.write(x) 

if len(sys.argv) != 2:
  print "Usage:",sys.argv[0], "<filename>"
  sys.exit(1)

isdict = dicts("islensku_ordalisti.txt")
endict = dicts("/usr/share/dict/words")
myfile = words(sys.argv[1])
p = processWords(isdict, endict, myfile)
print p.getReport()
p.getFrequency()
os.system("./makePlots")
